<template>
    <div class="cart">
        <h4>全部商品</h4>
        <div class="cart-main">
            <div class="cart-th">
                <div class="cart-th1">全部</div>
                <div class="cart-th2">商品</div>
                <div class="cart-th3">单价（元）</div>
                <div class="cart-th4">数量</div>
                <div class="cart-th5">小计（元）</div>
                <div class="cart-th6">操作</div>
            </div>
            <div class="cart-body">
                <ul
                    class="cart-list"
                    v-for="(item, index) in cartInfoList"
                    :key="item.id"
                >
                    <li class="cart-list-con1">
                        <input
                            type="checkbox"
                            name="chk_list"
                            :checked="item.isChecked == 1 ? true : false"
                            @change="changeCheck(index)"
                        />
                    </li>
                    <li class="cart-list-con2">
                        <img :src="item.imgUrl" />
                        <div class="item-msg">
                            {{ item.skuName }}
                        </div>
                    </li>
                    <li class="cart-list-con4">
                        <span class="price">{{ item.cartPrice }}</span>
                    </li>
                    <li class="cart-list-con5">
                        <a
                            href="javascript:void(0)"
                            class="mins"
                            @click="changeSkuNum('jian', -1, index)"
                            >-</a
                        >
                        <input
                            autocomplete="off"
                            type="text"
                            :value="item.skuNum"
                            minnum="1"
                            class="itxt"
                            @change="
                                changeSkuNum(
                                    'change',
                                    $event.target.value,
                                    index
                                )
                            "
                        />
                        <a
                            href="javascript:void(0)"
                            class="plus"
                            @click="changeSkuNum('jia', 1, index)"
                            >+</a
                        >
                    </li>
                    <li class="cart-list-con6">
                        <span class="sum">{{
                            item.skuNum * item.cartPrice
                        }}</span>
                    </li>
                    <li class="cart-list-con7">
                        <a
                            href="javascript:;"
                            class="sindelet"
                            @click="deleteItem(item.skuId)"
                            >删除</a
                        >
                        <br />
                        <br />
                        <a href="javascript:;">移到收藏</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="cart-tool">
            <div class="select-all">
                <input
                    ref="allChecked"
                    class="chooseAll"
                    type="checkbox"
                    :checked="allChecked&&cartInfoList.length!=0"
                    :disabled='cartInfoList.length==0'
                    @change="changeAllChecked"
                />
                <span>全选</span>
            </div>
            <div class="option">
                <a href="javascript:;" @click="deleteAllItemChecked"
                    >删除选中的商品</a
                >
                <a href="javascript:;">移到我的关注</a>
                <a href="javascript:;">清除下柜商品</a>
            </div>
            <div class="money-box">
                <div class="chosed">
                    已选择 <span>{{ totalCheacked }}</span
                    >件商品
                </div>
                <div class="sumprice">
                    <em>总价（不含运费） ：</em>
                    <i class="summoney">{{ totalPrize }}</i>
                </div>
                <div class="sumbtn">
                    <router-link clrouter-linkss="sum-btn" to="/trade">结算</router-link>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import throttle from "lodash/throttle";
import { mapGetters } from "vuex";
export default {
    name: "ShopCart",
    mounted() {
        this.getData();
    },
    computed: {
        ...mapGetters(["cartInfoList"]),
        totalPrize() {
            let sum = 0;
            this.cartInfoList.forEach((item) => {
                if (item.isChecked == 1) sum += item.skuNum * item.cartPrice;
            });
            return sum;
        },
        allChecked() {
            return this.cartInfoList.every((item) => {
                return item.isChecked == 1;
            });
        },
        totalCheacked() {
            let sum = 0;
            this.cartInfoList.forEach((item) => {
                if (item.isChecked == 1) {
                    sum++;
                }
            });
            return sum;
        },
    },
    methods: {
        // 获取个人购物车的数据
        getData() {
            this.$store.dispatch("getShopCartInfo");
        },
        // 修改数量时,需要发送请求到服务器保存当前的数据状态
        changeSkuNum: throttle(async function (type, a, index) {
            let gap = 0;
            if (type == "jia") {
                gap = a;
            } else if (type == "jian") {
                if (this.cartInfoList[index].skuNum < 1) {
                    gap = 0;
                } else {
                    gap = a;
                }
            } else {
                let res = a * 1;
                if (isNaN(res) || res < 0) {
                    res = this.cartInfoList[index].skuNum;
                } else {
                    res = parseInt(res);
                }
                gap = res - this.cartInfoList[index].skuNum;
            }
            this.cartInfoList[index].skuNum += gap;
            try {
                let res = await this.$store.dispatch("addShopCart", {
                    skuId: this.cartInfoList[index].skuId,
                    num: gap,
                });
                this.getData();
            } catch (e) {
                alert("修改数量失败");
            }
        }, 1000),
        changeCheck: throttle(async function (index) {
            this.cartInfoList[index].isChecked =
                1 - this.cartInfoList[index].isChecked;
            try {
                let res = await this.$store.dispatch("changeChecked", {
                    skuId: this.cartInfoList[index].skuId,
                    checked: this.cartInfoList[index].isChecked,
                });
                console.log(res);
                this.getData();
            } catch (e) {
                alert(e.message);
            }
        }, 500),
        // 删除物品
        async deleteItem(skuId) {
            try {
                let res = await this.$store.dispatch("delShopCartItem", skuId);
                alert(res);
                this.getData();
            } catch (e) {
                alert(e.message);
            }
        },
        // 全选或者全不选
        changeAllChecked: throttle(async function () {
            let res1 = this.$refs.allChecked.checked ? 1 : 0;
            try{
                let res=await this.$store.dispatch('AllItemChecked',res1);
                this.getData();
            }catch(e){
                alert(e.message);
            }
        }, 500),
        // 删除全部选中的商品
        async deleteAllItemChecked() {
            let res = confirm("确定要删除选中商品吗?");
            if (res) {
                try {
                    let res = await this.$store.dispatch(
                        "deleteAllItemChecked"
                    );
                    this.getData();
                } catch (e) {
                    console.log(e.message);
                }
            }
        },
    },
};
</script>

<style lang="less" scoped>
.cart {
    width: 1200px;
    margin: 0 auto;

    h4 {
        margin: 9px 0;
        font-size: 14px;
        line-height: 21px;
    }

    .cart-main {
        .cart-th {
            background: #f5f5f5;
            border: 1px solid #ddd;
            padding: 10px;
            overflow: hidden;

            & > div {
                float: left;
                // text-align: center;
            }

            .cart-th1 {
                width: 5%;
            }

            .cart-th2 {
                width: 35%;
            }

            .cart-th3,
            .cart-th4,
            .cart-th5,
            .cart-th6 {
                width: 15%;
            }
        }

        .cart-body {
            margin: 15px 0;
            border: 1px solid #ddd;

            .cart-list {
                padding: 10px;
                border-bottom: 1px solid #ddd;
                overflow: hidden;
                display: flex;
                align-items: center;

                & > li {
                    float: left;
                }

                .cart-list-con1 {
                    width: 5%;
                }

                .cart-list-con2 {
                    width: 35%;

                    img {
                        width: 82px;
                        height: 82px;
                        float: left;
                    }

                    .item-msg {
                        float: left;
                        width: 150px;
                        margin: 0 10px;
                        line-height: 18px;
                    }
                }

                .cart-list-con3 {
                    width: 15%;

                    .item-txt {
                        text-align: center;
                    }
                }

                .cart-list-con4 {
                    width: 15%;
                }

                .cart-list-con5 {
                    width: 15%;

                    .mins {
                        border: 1px solid #ddd;
                        border-right: 0;
                        float: left;
                        color: #666;
                        width: 6px;
                        text-align: center;
                        padding: 8px;
                    }

                    input {
                        border: 1px solid #ddd;
                        width: 40px;
                        height: 33px;
                        float: left;
                        text-align: center;
                        font-size: 14px;
                    }

                    .plus {
                        border: 1px solid #ddd;
                        border-left: 0;
                        float: left;
                        color: #666;
                        width: 6px;
                        text-align: center;
                        padding: 8px;
                    }
                }

                .cart-list-con6 {
                    width: 15%;

                    .sum {
                        font-size: 16px;
                    }
                }

                .cart-list-con7 {
                    width: 15%;

                    a {
                        color: #666;
                    }
                }
            }
        }
    }

    .cart-tool {
        overflow: hidden;
        border: 1px solid #ddd;
        display: flex;
        align-items: center;
        margin-bottom: 50px;

        .select-all {
            padding: 10px;
            overflow: hidden;
            float: left;

            span {
                vertical-align: middle;
            }

            input {
                vertical-align: middle;
            }
        }

        .option {
            padding: 10px;
            overflow: hidden;
            float: left;

            a {
                float: left;
                padding: 0 10px;
                color: #666;
            }
        }

        .money-box {
            float: right;
            display: flex;
            align-items: center;
            margin-left: 414px;

            .chosed {
                line-height: 26px;
                float: left;
                padding: 0 10px;
            }

            .sumprice {
                width: 200px;
                line-height: 22px;
                float: left;
                padding: 0 10px;

                .summoney {
                    color: #c81623;
                    font-size: 16px;
                }
            }

            .sumbtn {
                float: right;
                // margin-left: 414px;

                a {
                    display: block;
                    position: relative;
                    width: 96px;
                    height: 52px;
                    line-height: 52px;
                    color: #fff;
                    text-align: center;
                    font-size: 18px;
                    font-family: "Microsoft YaHei";
                    background: #e1251b;
                    overflow: hidden;
                }
            }
        }
    }
}
</style>